package com.zlzxm.shoesmarket.util;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.zlzxm.shoesmarket.R;

import java.math.BigDecimal;

public class KeyboardUtil {
    private KeyboardView keyboardView;
    private Keyboard k1;// 字母键盘
    private EditText ed;
    private Context ctx;
    BigDecimal oldBigDecimal = new BigDecimal(0) ;

    public KeyboardUtil( Context ctx,KeyboardView keyboardView, EditText edit) {
        this.ctx = ctx;
        this.ed = edit;
        k1 = new Keyboard(ctx, R.xml.number);
        this.keyboardView = keyboardView;
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(listener);
    }

    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {

        @Override
        public void swipeUp() {
        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = ed.getText();
            int start = ed.getSelectionStart();
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE:
                    if (editable != null && editable.length() > 0) {
                        if (start > 0) {
                            editable.delete(start - 1, start);
                        }
                    }
                    break;
                case Keyboard.KEYCODE_DONE:

//                    keyboardView.setVisibility(View.GONE);

                    if(editable.length()>0) {
                        BigDecimal bigDecimal = new BigDecimal(editable.toString().toCharArray());
                        editable.clear();
                        editable.insert(0, oldBigDecimal.add(bigDecimal).floatValue() + "");
                        oldBigDecimal =  new BigDecimal(0);
                    }
                    break;

                case 101:
                    if(editable.length()>0) {

                        oldBigDecimal = new BigDecimal(editable.toString().toCharArray());

                    }else {

                        oldBigDecimal =  new BigDecimal(0);
                    }

                    editable.clear();
                    break;

                default:
                    editable.insert(start, Character.toString((char) primaryCode));
                    break;
            }
        }
    };




    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.INVISIBLE);
        }
    }

    private boolean isword(String str){
        String wordstr = "abcdefghijklmnopqrstuvwxyz";
        if (wordstr.indexOf(str.toLowerCase())>-1) {
            return true;
        }
        return false;
    }

}

